package pile.pay.utils;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.Random;
import java.util.UUID;

public class ToolsUtils {

    private static final String RSA_ALGORITHM = "RSA";
    private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String CHARSET = "UTF-8";
    private static final String PRIVATE_kEY = "MIICWwIBAAKBgHCEJz8JuaiSMucb6JBtFuriyfQHZhcEDdbQfMyI/tegAbLQsThCBy4mvgyNcH8q2NRUosGXyX5WvkpI0rszFEcpwiqN8ecB6jjDDr1xI3O7wvxXoW6HCMgJclcS4LKGsQtLvS6N8XE5zuaFPlrUhAcUmVVRY9PP7tOnFhxutiVnAgMBAAECgYAYaBI7Uli7gDsadqtT6NX3zYoRLSmwBGkcF816cJ0XGNZFe491rJzyHeVLw8KLUzgFlvZXGB3VEevlITbU9JeJLnOe6L055h2tFS9CME1a/lWEU6UGor3Bv6mCq90ypDqHPWzdQJQfZL/S/aX7a6mDcg4jBL/OGeKLFtp9YidLAQJBALVCTt7L7xZLBwqX7ys8EVVdUL+PrAPHX06Vx+WDZD5qddpavUe04Axa+d69NJxz3rJREWv5DEHKEHhmt13GRfcCQQCe6VwcISq25wYpebLD0rlY/Xb9dyOdijlhMhc4UMyZlAb3JSrfo5KaPAhbJFp7+NXqZLLF0QNPm9AfdULW8IARAkEAjbj8xgMaO4oKRWQqYtmii+Zcfostaf73LBC8rV2ggZTn9QTBvlFUZRS1Dk1N145Zb38nag7eYIpHenyFntKqXQJAFF40wCZAI1FZKNfAYfkQaKeUKMisd/TPhfW1pDI3BzhB7EuHQ0Lj94DaUVRHhLxQLsrZXyF7GykNc/rDpmkNAQJAGrDK8zalyaYAfBeIofASaiPRUblbprtznN1FX7QhsxRfhA5OohcBh+9n67uLprCgtsMnE442I83McHm5hVC56Q==";


    /**
     * 生产唯一订单号
     *
     * @return
     */
    public static String uniqueId() {
        Random ne = new Random();
        int num = ne.nextInt(9999 - 1000 + 1) + 1000;
        String replaceUUID = UUID.randomUUID().toString().replace("-", "") + String.valueOf(num);
        String orderNo = replaceUUID.substring(4);
        return orderNo;
    }


    public static byte[] decrypt(String encryptedData, String key) throws Exception {

        String privateKeyStr = "MIICWwIBAAKBgHCEJz8JuaiSMucb6JBtFuriyfQHZhcEDdbQfMyI/tegAbLQsThCBy4mvgyNcH8q2NRUosGXyX5WvkpI0rszFEcpwiqN8ecB6jjDDr1xI3O7wvxXoW6HCMgJclcS4LKGsQtLvS6N8XE5zuaFPlrUhAcUmVVRY9PP7tOnFhxutiVnAgMBAAECgYAYaBI7Uli7gDsadqtT6NX3zYoRLSmwBGkcF816cJ0XGNZFe491rJzyHeVLw8KLUzgFlvZXGB3VEevlITbU9JeJLnOe6L055h2tFS9CME1a/lWEU6UGor3Bv6mCq90ypDqHPWzdQJQfZL/S/aX7a6mDcg4jBL/OGeKLFtp9YidLAQJBALVCTt7L7xZLBwqX7ys8EVVdUL+PrAPHX06Vx+WDZD5qddpavUe04Axa+d69NJxz3rJREWv5DEHKEHhmt13GRfcCQQCe6VwcISq25wYpebLD0rlY/Xb9dyOdijlhMhc4UMyZlAb3JSrfo5KaPAhbJFp7+NXqZLLF0QNPm9AfdULW8IARAkEAjbj8xgMaO4oKRWQqYtmii+Zcfostaf73LBC8rV2ggZTn9QTBvlFUZRS1Dk1N145Zb38nag7eYIpHenyFntKqXQJAFF40wCZAI1FZKNfAYfkQaKeUKMisd/TPhfW1pDI3BzhB7EuHQ0Lj94DaUVRHhLxQLsrZXyF7GykNc/rDpmkNAQJAGrDK8zalyaYAfBeIofASaiPRUblbprtznN1FX7QhsxRfhA5OohcBh+9n67uLprCgtsMnE442I83McHm5hVC56Q==";


        // 解析私钥字符串
        byte[] privateKeyBytes = Base64.getDecoder().decode(PRIVATE_kEY);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        // 将私钥转换为PKCS#8格式
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
        byte[] pkcs8EncodedBytes = pkcs8EncodedKeySpec.getEncoded();
        // 2. 从文件中读取加密后的AES密钥和密文
        byte[] encryptedKey = Base64.getDecoder().decode(key); // 加密后的AES密钥的字节数组
        byte[] data = Base64.getDecoder().decode(encryptedData); // 加密后的数据的字节数组

        // 3. 使用RSA解密AES密钥
        Cipher rsaCipher = Cipher.getInstance("RSA");
        rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] aesKeyBytes = rsaCipher.doFinal(encryptedKey);

        /*// 4. 使用AES解密数据
        Key aesKey = new SecretKeySpec(aesKeyBytes, "AES");
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
        byte[] decryptedData = aesCipher.doFinal(data);

        // 5. 将解密后的数据转换为字符串
        String decryptedText = new String(decryptedData, "UTF-8");*/

        return aesKeyBytes;
    }




}
